package org.calzz.example;

import java.lang.reflect.Array;

import org.calzz.jajb.JsonDeserializer;
import org.calzz.jajb.JsonSerializer;
import org.calzz.jajb.converter.IConverter;
import org.calzz.jajb.io.IWriter;
import org.springframework.core.MethodParameter;

public class CustomArrayConverter implements IConverter {

	@Override
	public boolean canConvert(Class<?> clazz) {
		return clazz.isArray();
	}

	@Override
	public void marshal(Object obj, String name, IWriter writer, JsonSerializer serializer) {
		int length = Array.getLength(obj);
		writer.startObject(name);
		for (int i = 0; i < length; i++) {
			serializer.process(Array.get(obj, i), "item" + i, writer);
		}
		writer.endObject();
	}

	@Override
	public <T> Object unmarshal(Object env, Class<T> clazz, JsonDeserializer deserializer, MethodParameter methodParam) {
		// TODO Auto-generated method stub
		return null;
	}

}
